package com.java.comprehensive.arithmetic.leecode.easy;

/**
 * @program: comprehensive-service
 *
 * @description: 两数之和
 *
 * @author: 窦绍飞
 *
 * @create: 2021-10-29 11:26
 **/

public class LeetCode_02 {
	static class ListNode {
		int val;
		ListNode next;

		ListNode() {
		}

		ListNode(int val) {
			this.val = val;
		}

		ListNode(int val, ListNode next) {
			this.val = val;
			this.next = next;
		}
	}

	static class Solution {
		public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
			int num = 0;
			int sum = 0;
			ListNode pre = new ListNode();
			ListNode curr = new ListNode();
			pre.next = curr;
			while (l1 != null || l2 != null || num != 0) {
				int l1Val = l1 != null ? l1.val : 0;
				int l2Val = l2 != null ? l2.val : 0;
				sum = l1Val + l2Val + num;
				num = sum / 10;
				curr.val = sum % 10;
				l1 = l1 != null ? l1.next : l1;
				l2 = l2 != null ? l2.next : l2;
				if (l1 != null || l2 != null || num != 0) {
					curr.next = new ListNode(0);
					curr = curr.next;
				}
			}
			return pre.next;
		}
	}

	public static void main(String[] args) {
		ListNode listNode1 = new ListNode(2);
		ListNode listNode2 = new ListNode(4);
		ListNode listNode3 = new ListNode(3);
		ListNode listNode4 = new ListNode(5);
		ListNode listNode5 = new ListNode(6);
		ListNode listNode6 = new ListNode(4);
		listNode2.next = listNode3;
		listNode1.next = listNode2;
		ListNode l1 = listNode1;
		listNode5.next = listNode6;
		listNode4.next = listNode5;
		ListNode l2 = listNode4;
		System.out.println(Solution.addTwoNumbers(l1, l2));
	}
}
